/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						     dialog_war_institute.cpp

	$Header: /game/dialog_war_institute.cpp $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "dialog_war_institute.h"

#include "army.h"
#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer_window.h"
#include "bound_handler.h"
#include "button.h"
#include "button_cache.h"
#include "creature_select_window.h"
#include "external_string.h"
#include "format_string.h"
#include "game_replace_keywords.h"
#include "hero.h"
#include "interface_help.h"
#include "player.h"
#include "random.h"
#include "skill.h"
#include "skill_properties.h"
#include "standard_fonts.h"
#include "text_window.h"
#include "war_institute.h"

const   int k_skill_upgrade_cost    = 2000;

extern t_external_string const		k_hotkey_buy;
extern t_button_cache const         k_learn_button;
extern t_button_cache const			k_close_button;
extern t_button_cache			    g_ok_button;
extern t_button_cache const         k_buy_button;

static t_bitmap_group_cache const	k_war_institute_bitmaps( "dialog.war_institute" );
t_bitmap_group_cache const   k_large_material_bitmaps( "icons.materials.64" );

/*
** War institute/Veteran's Guild dialog constructor.
*/
t_dialog_war_institute::t_dialog_war_institute ( t_window* parent )
					: t_window( k_completely_transparent, parent )
{
}

/*
** Init dialog for use with the War Institute.
** - If at least one hero in the party is eligible for a skill upgrade, show the
**   hero(oes) here, and allow the user to pick the hero(oes) to upgrade.
*/
void t_dialog_war_institute::init_dialog ( std::vector<t_hero*> const& heroes, 
										   std::vector<t_hero*> const& selectable_heroes,
                                           t_single_use_object const& parent_object,
										   t_window* parent, t_army* army )
{
	t_screen_rect           rect;
	t_bitmap_layer const*   layer;
	t_window*               window;
	t_window*               top_window;
	t_window*               background;
	t_screen_point          origin(0,0);
	t_screen_point          point;
	std::vector<t_skill>    upgradable_skills;
	int                     i;
	t_bitmap_group_ptr      material_bitmaps_ptr = k_large_material_bitmaps.get();
	t_help_block const&		help = get_help_block( "shared" );
    
	m_player_ptr = army->get_owner();
    m_parent_object = &parent_object;
	m_current_cost = 0;
	m_player_funds = m_player_ptr->get_funds()[k_gold];
    
	m_bitmaps_ptr = k_war_institute_bitmaps.get();

	/*
    ** Create background
    */
	layer = m_bitmaps_ptr->find( "top" );
	top_window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps_ptr->find( "left" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps_ptr->find( "bottom" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps_ptr->find( "right" );
	window = new t_bitmap_layer_window( layer, origin, this );

	layer = m_bitmaps_ptr->find( "background" );
	background = new t_bitmap_layer_window( layer, origin, this );
	origin = -layer->get_rect().top_left();

    // Get gold icon.
    layer = material_bitmaps_ptr->find ("gold");
    
	rect = m_bitmaps_ptr->find( "gold_icon" )->get_rect() + origin;
    point = rect.top_left() + origin;
    point.x += abs((rect.width ()/2) - (layer->get_width()/2));
	window = new t_bitmap_layer_window( layer, point, background ); //this );

    /*
    ** Text windows.
    */
	t_text_window* text_window_ptr;
	std::string    text;

	/*
    ** Add the title text.
    */
	text = m_parent_object->get_text("name");
	rect = m_bitmaps_ptr->find( "text_title" )->get_rect(); // + origin;
	text_window_ptr = new t_text_window( get_font( 22 ), rect, this, text, t_pixel_24(0,0,0));
	text_window_ptr->set_center_horizontal();
	text_window_ptr->set_drop_shadow( false );

	/*
    ** Add the intro text.
    */
	text = m_parent_object->get_text("initial");
	rect = m_bitmaps_ptr->find( "text_dialog" )->get_rect() + origin;
	text_window_ptr = new t_text_window( get_font( 18 ), rect, background, text, t_pixel_24(0,0,0));
	text_window_ptr->set_center_horizontal();
	text_window_ptr->set_drop_shadow( false );

	/*
    ** Create hero selection portraits. 
    */

	point = m_bitmaps_ptr->find("hero_portraits")->get_rect().top_left() + origin;

	m_hero_selection_window = new t_creature_select_window( point, false, background );
	for (i = 0; i < (int)heroes.size(); i++)
	{
		m_hero_selection_window->add( heroes[i] );
		
        if ( std::find(selectable_heroes.begin(), selectable_heroes.end(), heroes[i]) == selectable_heroes.end())
        {
			/*
			** No upgradable skills, or hero visited already.
			*/
		    m_hero_selection_window->enable( i, false );
            m_hero_selection_window->set_help_text( i, m_parent_object->get_text("help.rejected") );
        }
        else
        {
		    m_hero_selection_window->set_help_text( i, heroes[i]->get_name() );
            // Set cost.
	        m_current_cost += k_skill_upgrade_cost;
        }
	}
	m_hero_selection_window->select_all();
	m_hero_selection_window->set_handler( bound_handler( *this, &t_dialog_war_institute::selection_change ));
    
    /*
    ** Add the gold cost text - cost of skill upgrade is 2000
    */
	rect = m_bitmaps_ptr->find( "text_gold" )->get_rect() + origin;
	m_cost_window_ptr = new t_text_window( get_font( rect.height() ), rect, background, format_string( "%i", m_current_cost ), 
		                             t_pixel_24(0,0,0) );
	m_cost_window_ptr->set_drop_shadow( true, t_pixel_24(200,200,200) );
	m_cost_window_ptr->set_justification( k_justify_center );

	/*
    ** Add buttons.
    */
	t_button*        button;
	t_button_handler button_handler;

	point = m_bitmaps_ptr->find( "purchase_icon" )->get_rect().top_left() + origin;
	m_buy_button = new t_button( k_learn_button.get(), point, background );
	button_handler = bound_handler( *this, &t_dialog_war_institute::buy_clicked );
	m_buy_button->set_click_handler( button_handler );
  set_help( m_buy_button, help, "learn" );

	point = m_bitmaps_ptr->find( "close_button" )->get_rect().top_left() + origin;
	button = new t_button( g_ok_button.get(), point, background );
	button_handler = bound_handler( *this, &t_dialog_war_institute::close_clicked );
	button->set_click_handler( button_handler );
  set_help( button , help, "ok" );

    // Init.
    selection_change( NULL, NULL );

    /*
    ** Set the position and display the window.
    */
	t_screen_rect parent_rect = get_parent()->get_client_rect();
    
	rect = m_bitmaps_ptr->get_rect();
	rect += (parent_rect.size() - rect.size()) / 2;
	init( rect );
}

// ------------------------------------------------------------------
// War institute dialog - select/deselect hero.
// ------------------------------------------------------------------
void t_dialog_war_institute::selection_change( t_creature_select_window*, t_creature_stack* )
{
	int i;

	m_current_cost = 0;

	for (i = 0; i < m_hero_selection_window->get_count(); i++)
    {
		if ((m_hero_selection_window->is_selected(i)) && (m_hero_selection_window->is_enabled (i)))
        {
	        m_current_cost += k_skill_upgrade_cost;
        }
    }

    /*
    ** Set current cost of upgrades.
    */
	m_cost_window_ptr->set_text( format_string( "%i", m_current_cost ) );

    /*
    ** Disable buy button if player doesn't have sufficient funds.
    */
	if ((m_current_cost > m_player_funds) || (m_current_cost == 0)) 
    {
	    m_buy_button->enable (false);
    }
    else
    {
	    m_buy_button->enable (true);
    }
}

/*-------------------------------------------------------------------*\
** Buy skill upgrade.
\*-------------------------------------------------------------------*/
void t_dialog_war_institute::buy_clicked( t_button* button )
{
	int                     i;
	t_hero*                 hero;
    
	m_selected_heroes.clear();
    
	m_return_value = false;
	for (i = 0; i < m_hero_selection_window->get_count(); i++)
	{
		if (!m_hero_selection_window->is_selected(i))
			continue;

		if (!m_hero_selection_window->is_enabled (i))
            continue;

		hero = m_hero_selection_window->get_creature(i)->get_hero();

        // Add selected hero to upgrade list.
		m_selected_heroes.push_back( hero );
	    m_return_value = true;
	}
    
	if (m_selected_heroes.size() == 0)
        return;
    
    // Spend the gold...Easy come, easy go...
	m_player_ptr->spend( k_gold, m_current_cost );
	close();
}

/*-------------------------------------------------------------------*\
** Close the war institute's dialog
\*-------------------------------------------------------------------*/
void t_dialog_war_institute::close_clicked( t_button* button )
{
	m_return_value = false;
	close();
}

// Return selected heroes for skill upgrade.
std::vector<t_hero*>t_dialog_war_institute::get_selected_heroes()
{
    return (m_selected_heroes);
}

